<template>
	<view class="content">
		<!-- 导航栏 -->
		<u-navbar :is-back="true" :title="title" :background="background" :title-color="titleColor"></u-navbar>
		<view class="wrap">
			<view class="u-tabs-box">
				<u-tabs-swiper activeColor="#f29100" ref="tabs" :list="list" :current="current" @change="change"
					:is-scroll="false" swiperWidth="750"></u-tabs-swiper>
			</view>
			<swiper class="swiper-box" :current="swiperCurrent" @transition="transition"
				@animationfinish="animationfinish">
				<swiper-item class="swiper-item">
					<scroll-view scroll-y style="height: 100%;width: 100%;" @scrolltolower="reachBottom">
						<view class="page-box">
							<view v-if="dataList1.length > 0">
								<view class="order" v-for="(res, index) in  dataList1" :key="res.orderId">
									<view class="top">
										<view class="left">
											<u-icon name="home" :size="30" color="rgb(94,94,94)"></u-icon>
											<view class="store">{{res.iuserName}}</view>
											<u-icon name="arrow-right" color="rgb(203,203,203)" :size="26"></u-icon>
										</view>
										<view class="right">待确认</view>
									</view>
									<view class="item" @click="onClick($event,res.comId)">
										<view class="left">
											<image :src="'http://localhost:8088'+res.imgUrl.split(',')[0]"
												mode="aspectFill"></image>
										</view>
										<view class="content">
											<view class="title u-line-2">{{res.comName}}</view>
											<view class="type">{{res.typeName}}</view>
											<view class="delivery-time">下单时间
												{{$u.timeFormat(res.createTime, 'yyyy-mm-dd')}}
											</view>
										</view>
										<view class="right">
											<view class="price">
												￥{{ priceInt(res.soldMoney) }}
												<text class="decimal">.{{ priceDecimal(res.soldMoney) }}</text>
											</view>
											<view class="number">{{ res.oriMoney }}</view>
										</view>
									</view>
									<view class="bottom">
										<view class="exchange btn" @click="toFail(res.orderId,res.comId)">取消</view>
										<view class="evaluate btn" @click="toSuccess(res.orderId)">确认完成</view>
									</view>
								</view>
								<u-loadmore :status="loadStatus[0]" bgColor="#f2f2f2"></u-loadmore>
							</view>
							<view v-if="dataList1.length <= 0">
								<view>
									<view class="centre">
										<image src="https://cdn.uviewui.com/uview/template/taobao-order.png" mode="">
										</image>
										<view class="explain">
											您还没有相关的订单
											<view class="tips">可以去看看有那些想买的</view>
										</view>
										<view class="btn" @click="toSearch">随便逛逛</view>
									</view>
								</view>
							</view>
						</view>
					</scroll-view>
				</swiper-item>
				<swiper-item class="swiper-item">
					<scroll-view scroll-y style="height: 100%;width: 100%;" @scrolltolower="reachBottom">
						<view class="page-box">
							<view v-if="dataList2.length > 0">
								<view class="order" v-for="(res, index) in  dataList2" :key="res.orderId">
									<view class="top">
										<view class="left">
											<u-icon name="home" :size="30" color="rgb(94,94,94)"></u-icon>
											<view class="store">{{res.iuserName}}</view>
											<u-icon name="arrow-right" color="rgb(203,203,203)" :size="26"></u-icon>
										</view>
										<view class="right">已完成</view>
									</view>
									<view class="item" @click="onClick($event,res.comId)">
										<view class="left">
											<image :src="'http://localhost:8088'+res.imgUrl.split(',')[0]"
												mode="aspectFill"></image>
										</view>
										<view class="content">
											<view class="title u-line-2">{{res.comName}}</view>
											<view class="type">{{res.typeName}}</view>
											<view class="delivery-time">下单时间
												{{$u.timeFormat(res.createTime, 'yyyy-mm-dd')}}
											</view>
										</view>
										<view class="right">
											<view class="price">
												￥{{ priceInt(res.soldMoney) }}
												<text class="decimal">.{{ priceDecimal(res.soldMoney) }}</text>
											</view>
											<view class="number">{{ res.oriMoney }}</view>
										</view>
									</view>
									<view class="bottom">
										<view class="exchange btn">删除</view>
										<view v-if="res.evaluate == null" class="evaluate btn"
											@click="evaluate(res.orderId)">评价</view>
										<view v-else class="evaluate btn" @click="look(res.evaluate)">查看评价</view>
									</view>
								</view>
								<u-modal v-model="show1" :title-style="{color: 'red'}"
									:content-style="{padding: '35rpx 20rpx'}" title="评论">
									<view class="slot-content">
										<rich-text :nodes="content"></rich-text>
									</view>
								</u-modal>
								<u-modal v-model="show2" :title-style="{color: 'red'}"
									:content-style="{padding: '35rpx 20rpx'}" @confirm="confirm" title="评论">
									<view class="slot-content">
										<u-input v-model="inputValue" type="text" />
									</view>
								</u-modal>
								<u-loadmore :status="loadStatus[0]" bgColor="#f2f2f2"></u-loadmore>
							</view>
							<view v-if="dataList2.length <= 0">
								<view>
									<view class="centre">
										<image src="https://cdn.uviewui.com/uview/template/taobao-order.png" mode="">
										</image>
										<view class="explain">
											您还没有相关的订单
											<view class="tips">可以去看看有那些想买的</view>
										</view>
										<view class="btn" @click="toSearch()">随便逛逛</view>
									</view>
								</view>
							</view>
						</view>
					</scroll-view>
				</swiper-item>
			</swiper>
		</view>
	</view>
</template>

<script>
	import Vue from 'vue'
	import {
		mapGetters
	} from 'vuex'
	export default {
		data() {
			return {
				title: "我的订单",
				titleColor: "#330867",
				background: {
					// 渐变色
					backgroundImage: 'linear-gradient(to right, #f78ca0 0%, #f9748f 19%, #fd868c 60%, #fe9a8b 100%)'
				},

				list: [{
						name: '待确认'
					},
					{
						name: '已完成'
					}
				],
				dataList1: [],
				dataList2: [],
				show1: false,
				show2: false,
				content: '',
				inputValue: '',
				orderId: '',
				// 查询参数
				queryParams1: {
					pageNum: 1,
					pageSize: 10,
					orderStatus: '0'
				},
				// 查询参数
				queryParams2: {
					pageNum: 1,
					pageSize: 10,
					orderStatus: '1'
				},
				pageTotal1: 0,
				pageTotal2: 0,
				current: 0,
				swiperCurrent: 0,
				tabsHeight: 0,
				dx: 0,
				loadStatus: ['loadmore', 'loadmore']
			}
		},
		computed: {
			...mapGetters(['getUserInfo']),
			// 价格小数
			priceDecimal() {
				return val => {
					if (val !== parseInt(val)) return val.slice(-2);
					else return '00';
				};
			},
			// 价格整数
			priceInt() {
				return val => {
					if (val !== parseInt(val)) return val.split('.')[0];
					else return val;
				};
			}
		},
		onLoad() {
			this.getDataList1();
			this.getDataList2();
		},
		methods: {
			getDataList1() {
				this.queryParams1.pageNum = 1;
				Vue.prototype.$u.api.selectMyOrder({
					pageNum: this.queryParams1.pageNum,
					pageSize: this.queryParams1.pageSize,
					userId: this.getUserInfo.userId,
					orderStatus: this.queryParams1.orderStatus
				}).then(res => {
					if (res.total < this.queryParams1.pageSize) {
						this.loadStatus[0] = 'nomore'
					}
					this.pageTotal1 = Math.ceil(res.total / this.queryParams1.pageSize);
					if (res.code == 200 && res.rows.length > 0) {
						this.dataList1 = res.rows
					} else {
						this.dataList1 = []
					}
				})
			},
			getDataList2() {
				this.queryParams2.pageNum = 1;
				Vue.prototype.$u.api.selectMyOrder({
					pageNum: this.queryParams2.pageNum,
					pageSize: this.queryParams2.pageSize,
					userId: this.getUserInfo.userId,
					orderStatus: this.queryParams2.orderStatus
				}).then(res => {
					if (res.total < this.queryParams2.pageSize) {
						this.loadStatus[1] = 'nomore'
					}
					this.pageTotal2 = Math.ceil(res.total / this.queryParams2.pageSize);
					if (res.code == 200 && res.rows.length > 0) {
						this.dataList2 = res.rows
					} else {
						this.dataList2 = []
					}
				})
			},
			reachBottom() {
				if (this.current == 0) {
					this.loadStatus[0] = 'loading';
					this.queryParams1.pageNum = ++this.queryParams1.pageNum;
					if (this.queryParams1.pageNum <= this.pageTotal1) {
						setTimeout(() => {
							Vue.prototype.$u.api.selectMyOrder({
								pageNum: this.queryParams1.pageNum,
								pageSize: this.queryParams1.pageSize,
								userId: this.getUserInfo.userId,
								orderStatus: this.queryParams1.orderStatus
							}).then(res => {
								if (res.code == 200 && res.rows.length > 0) {
									this.dataList1 = this.dataList1.concat(res.rows)
									this.loadStatus[0] = 'loadmore';
								} else {
									this.loadStatus[0] = 'nomore'
								}
							})
						}, 2000)
					} else {
						this.loadStatus[0] = 'nomore'
					}
				} else {
					this.loadStatus[1] = 'loading';
					this.queryParams2.pageNum = ++this.queryParams2.pageNum;
					if (this.queryParams2.pageNum <= this.pageTotal2) {
						setTimeout(() => {
							Vue.prototype.$u.api.selectMyOrder({
								pageNum: this.queryParams2.pageNum,
								pageSize: this.queryParams2.pageSize,
								userId: this.getUserInfo.userId,
								orderStatus: this.queryParams2.orderStatus
							}).then(res => {
								if (res.code == 200 && res.rows.length > 0) {
									this.dataList2 = this.dataList2.concat(res.rows)
									this.loadStatus[1] = 'loadmore';
								} else {
									this.loadStatus[1] = 'nomore'
								}
							})
						}, 2000)
					} else {
						this.loadStatus[1] = 'nomore'
					}
				}
			},
			onClick(event, comId) {
				uni.navigateTo({
					url: '/pages/detail/detail?comId=' + comId
				});
			},
			// 评论
			evaluate(orderId) {
				this.show2 = true;
				this.orderId = orderId;
			},
			// 查看评论
			look(evaluate) {
				this.show1 = true;
				this.content = evaluate
			},
			// 监听模态框的确认
			confirm(orderId) {
				Vue.prototype.$u.api.evaluateOrder({
					orderId: this.orderId,
					evaluate: this.inputValue
				}).then(res => {
					if (res.code == "200") {
						Vue.prototype.$u.toast(res.msg);
						this.getDataList2()
					} else {
						Vue.prototype.$u.toast(res.msg);
					}
				})
			},
			// 跳转到搜索页面
			toSearch() {
				uni.navigateTo({
				 url: '/pages/search/search'
				});
			},
			// 取消订单
			toFail(orderId,comId){
				let that = this
				uni.showModal({
				    title: '提示',
				    content: '是否取消订单',
				    success: function (res) {
				        if (res.confirm) {
				            Vue.prototype.$u.api.cancelOrder({
								orderId: orderId,
								comId: comId
							}).then(res => {
								if(res.code == 200){
									that.getDataList1();
									Vue.prototype.$u.toast("取消成功！");
								}else{
									Vue.prototype.$u.toast("取消失败！");
								}
							})
				        } else if (res.cancel) {
				            console.log('用户点击取消');
				        }
				    }
				});
			},
			// 完成订单
			toSuccess(orderId){
				let that = this
				uni.showModal({
				    title: '提示',
				    content: '是否确认完成交易',
				    success: function (res) {
				        if (res.confirm) {
				            Vue.prototype.$u.api.finishOrder({
								orderId: orderId
							}).then(res => {
								if(res.code == 200){
									that.getDataList1();
									that.getDataList2();
									Vue.prototype.$u.toast("确认成功！");
								}else{
									Vue.prototype.$u.toast("确认失败！");
								}
							})
				        } else if (res.cancel) {
				            console.log('用户点击取消');
				        }
				    }
				});
			},
			// tab栏切换
			change(index) {
				this.swiperCurrent = index;
			},
			transition({
				detail: {
					dx
				}
			}) {
				this.$refs.tabs.setDx(dx);
			},
			animationfinish({
				detail: {
					current
				}
			}) {
				this.$refs.tabs.setFinishCurrent(current);
				this.swiperCurrent = current;
				this.current = current;
			}
		}
	}
</script>

<style scoped lang="scss">
	.order {
		width: 710rpx;
		background-color: #ffffff;
		margin: 20rpx auto;
		border-radius: 20rpx;
		box-sizing: border-box;
		padding: 20rpx;
		font-size: 28rpx;

		.top {
			display: flex;
			justify-content: space-between;

			.left {
				display: flex;
				align-items: center;

				.store {
					margin: 0 10rpx;
					font-size: 32rpx;
					font-weight: bold;
				}
			}

			.right {
				color: $u-type-warning-dark;
			}
		}

		.item {
			display: flex;
			margin: 20rpx 0 0;

			.left {
				margin-right: 20rpx;

				image {
					width: 200rpx;
					height: 200rpx;
					border-radius: 10rpx;
				}
			}

			.content {
				.title {
					font-size: 28rpx;
					line-height: 50rpx;
				}

				.type {
					margin: 10rpx 0;
					font-size: 24rpx;
					color: $u-tips-color;
				}

				.delivery-time {
					color: #e5d001;
					font-size: 24rpx;
				}
			}

			.right {
				margin-left: 10rpx;
				padding-top: 20rpx;
				text-align: right;

				.decimal {
					font-size: 24rpx;
					margin-top: 4rpx;
				}

				.number {
					color: $u-tips-color;
					font-size: 24rpx;
				}
			}
		}

		.bottom {
			display: flex;
			margin-top: 40rpx;
			padding: 0 10rpx;
			justify-content: space-between;
			align-items: center;

			.btn {
				line-height: 52rpx;
				width: 160rpx;
				border-radius: 26rpx;
				border: 2rpx solid $u-border-color;
				font-size: 26rpx;
				text-align: center;
				color: $u-type-info-dark;
			}

			.evaluate {
				color: $u-type-warning-dark;
				border-color: $u-type-warning-dark;
			}
		}
	}

	.centre {
		text-align: center;
		margin: 200rpx auto;
		font-size: 32rpx;

		image {
			width: 164rpx;
			height: 164rpx;
			border-radius: 50%;
			margin-bottom: 20rpx;
		}

		.tips {
			font-size: 24rpx;
			color: #999999;
			margin-top: 20rpx;
		}

		.btn {
			margin: 80rpx auto;
			width: 200rpx;
			border-radius: 32rpx;
			line-height: 64rpx;
			color: #ffffff;
			font-size: 26rpx;
			background: linear-gradient(270deg, rgba(249, 116, 90, 1) 0%, rgba(255, 158, 1, 1) 100%);
		}
	}

	.wrap {
		display: flex;
		flex-direction: column;
		height: calc(100vh - var(--window-top));
		width: 100%;
	}

	.swiper-box {
		flex: 1;
	}

	.swiper-item {
		height: 100%;
	}
</style>
